"""
用两个栈实现一个队列。队列的声明如下，请实现它的两个函数 appendTail 和 deleteHead ，分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素，deleteHead 操作返回 -1 )

 

示例 1：

输入：
["CQueue","appendTail","deleteHead","deleteHead"]
[[],[3],[],[]]
输出：[null,null,3,-1]
示例 2：

输入：
["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
[[],[],[5],[2],[],[]]
输出：[null,-1,null,null,5,2]
提示：

1 <= values <= 10000
最多会对 appendTail、deleteHead 进行 10000 次调用

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。


使用栈实现队列的下列操作：

push(x) -- 将一个元素放入队列的尾部。
pop() -- 从队列首部移除元素。
peek() -- 返回队列首部的元素。
empty() -- 返回队列是否为空。
 

示例:

MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek();  // 返回 1
queue.pop();   // 返回 1
queue.empty(); // 返回 false
 

说明:

你只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque（双端队列）来模拟一个栈，只要是标准的栈操作即可。
假设所有操作都是有效的 （例如，一个空的队列不会调用 pop 或者 peek 操作）。

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/implement-queue-using-stacks
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
"""


class CQueue:

    def __init__(self):
        self.head, self.tail = [], []

    def appendTail(self, value: int) -> None:
        self.tail.append(value)
        return None

    def deleteHead(self) -> int:
        if self.head:
            return self.head.pop()
        if not self.tail:
            return -1
        while self.tail:
            self.head.append(self.tail.pop())
        return self.head.pop()

    def push(self, value: int) -> None:
        self.tail.append(value)
        return None

    def pop(self) -> int:
        if self.head:
            return self.head.pop()
        if not self.tail:
            return -1
        while self.tail:
            self.head.append(self.tail.pop())
        return self.head.pop()

    def peek(self) -> int:
        """
        Get the front element.
        """
        if self.head:
            elem = self.head.pop()
            self.head.append(elem)
            return elem
        if len(self.tail) == 1:
            elem = self.tail.pop()
            self.head.append(elem)
            return elem
        while self.tail:
            self.head.append(self.tail.pop())
        elem = self.head.pop()
        self.head.append(elem)
        return elem

    def empty(self) -> bool:
        """
        Returns whether the queue is empty.
        """
        return len(self.tail) == 0 and len(self.head) == 0


if __name__ == '__main__':
    obj = CQueue()
    # a = ["deleteHead","appendTail","deleteHead","deleteHead","deleteHead","deleteHead","appendTail",
    #      "deleteHead","appendTail","appendTail","deleteHead","deleteHead","deleteHead","appendTail",
    #      "deleteHead","deleteHead","deleteHead","deleteHead","appendTail","appendTail","appendTail",
    #      "appendTail","deleteHead","deleteHead","appendTail","appendTail","deleteHead","deleteHead",
    #      "deleteHead","appendTail","appendTail","deleteHead","appendTail","deleteHead","appendTail",
    #      "appendTail","appendTail","deleteHead","deleteHead","appendTail","appendTail","deleteHead",
    #      "deleteHead","deleteHead","deleteHead","appendTail","appendTail","deleteHead","deleteHead",
    #      "appendTail","deleteHead","appendTail","appendTail","appendTail","appendTail","deleteHead",
    #      "appendTail","deleteHead","deleteHead","appendTail","appendTail","appendTail","deleteHead",
    #      "deleteHead","appendTail","appendTail","appendTail","deleteHead","deleteHead","deleteHead",
    #      "deleteHead","appendTail","appendTail","deleteHead","deleteHead","appendTail","deleteHead",
    #      "appendTail","appendTail","deleteHead","deleteHead","appendTail","deleteHead","appendTail",
    #      "appendTail","appendTail","deleteHead","appendTail","appendTail","appendTail","appendTail",
    #      "deleteHead","deleteHead","deleteHead","appendTail","deleteHead","appendTail","deleteHead",
    #      "appendTail","appendTail"]
    # b = [[],[97],[],[],[],[],[15],[],[1],[43],[],[],[],[18],[],[],[],[],[36],[69],[21],[91],[],[],[22],
    #      [40],[],[],[],[81],[65],[],[77],[],[63],[96],[5],[],[],[35],[90],[],[],[],[],[77],[83],[],[],
    #      [52],[],[2],[66],[87],[90],[],[2],[],[],[33],[16],[72],[],[],[14],[78],[8],[],[],[],[],[3],
    #      [83],[],[],[13],[],[79],[44],[],[],[33],[],[55],[76],[12],[],[91],[24],[49],[47],[],[],[],[85],
    #      [],[69],[],[94],[52]]
    # n = len(a)
    # for i in range(33):
    #     c = getattr(obj, a[i])
    #     if a[i] == 'deleteHead':
    #         print(f"{i}.del=>{c()}")
    #     else:
    #         c(b[i][0])
    #         print(f"{i}.add=>{b[i][0]}", )

    # obj.deleteHead()
    # obj.appendTail(1)
    # obj.appendTail(2)
    # obj.appendTail(3)
    # print(obj.deleteHead())
    # obj.appendTail(4)
    # print(obj.deleteHead())
    # print(obj.deleteHead())
    # obj.appendTail(5)
    # obj.appendTail(6)
    # print(obj.deleteHead())
    obj.push(1)
    obj.push(2)
    print(obj.peek())
    obj.push(3)
    obj.push(4)
    print(obj.peek())
    print(obj.pop())
    print(obj.empty())